var sex;//性别
var marriage;//婚姻
var political;//政治面貌
var education;//文化程度
var type;//招聘类型
var resume;//简历
$(function () {
    type = $("input[name='type']").val();
    if (type === "校招") {
        $('title').text('广联官方网站 招贤纳士 校园招聘');
        $(".introduction-category-item a").eq(2).addClass('bottom-line');
    } else {
        $('title').text('广联科方网站 招贤纳士 社会招聘');
        $(".introduction-category-item a").eq(3).addClass('bottom-line')
    }
    $(".apply-data").hide();


    $('.sex').selectFilter({
        callBack: function (val) {
            //返回选择的值
            sex = val;
            console.log("性别：" + sex)
        }
    });
    $('.marriage').selectFilter({

        callBack: function (val) {
            //返回选择的值
            marriage = val;
            console.log("婚姻：" + marriage)
        }
    });
    $('.political').selectFilter({

        callBack: function (val) {
            //返回选择的值
            political = val;
            console.log("政治面貌：" + political)
        }
    });
    $('.education').selectFilter({

        callBack: function (val) {
            //返回选择的值
            education = val
            console.log("文化程度：" + education)
        }
    });
    $(".filter-list").hide();

})

//点击申请按钮
$(".apply").click(function (e) {
    $(".apply-data").toggle("fast");
    $(".apply").hide();
});


//上传简历
layui.use('upload', function () {
    var upload = layui.upload;
    //执行实例
    var uploadInst = upload.render({
        elem: '#LAY_avatarUpload' //绑定元素
        ,
        size: 2048 //文件大小限制
        ,
        number: 1 //文件数量限制
        ,
        accept: 'file' //文件类型限制
        ,
        acceptMime: 'application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/pdf,aplication/zip'
        ,
        exts: 'doc|docx|pdf|zip'
        ,
        url: '/upload/resume/addFile' //上传接口
        ,
        before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.msg('上传中');
            obj.preview(function (index, file, result) {
                // var files = obj.pushFile();
                console.log(file)
            });
        }
        ,
        done: function (res, index, upload) {
            layer.closeAll('loading');
            if (res.code === 0) {
                layer.msg(res.msg);
                console.log(res.name)
                $(".resume-file").text(res.name)
                resume = res.data;
            }

            //获取当前触发上传的元素，一般用于 elem 绑定 class 的情况，注意：此乃 layui 2.1.0 新增
            var item = this.item;
            //文件保存失败
            //do something
        }
        ,
        error: function () {
            layer.closeAll('loading');
            //请求异常回调
            layer.msg("上传失败，请重试");
        }

    });
});


$("#submitBtn").click(function (e) {
    var id = $("input[name='id']").val();//招聘id
    var recruitmentName = $("input[name='recruitmentName']").val();//招聘职位
    var name = $("input[name='name']").val();//姓名
    var mail = $("input[name='mail']").val();//邮箱
    var hometown = $("input[name='hometown']").val();//籍贯
    var school = $("input[name='school']").val();//学校
    var professional = $("input[name='professional']").val();//专业
    var date = $("input[name='date']").val();//毕业时间
    var language = $("input[name='language']").val();//外语
    var phone = $("input[name='phone']").val();//电话

    if (!vaildeParam(name)) {
        layer.msg("姓名不能为空");
        return;
    }
    if (!vaildeParam(sex)) {
        layer.msg("请选择性别");
        return;
    }
    if (!vaildeParam(marriage)) {
        layer.msg("请选择婚姻");
        return;
    }
    if (!vaildeParam(mail)) {
        layer.msg("邮箱不能为空");
        return;
    }

    if (!vaildeEmail(mail)) {
        layer.msg("邮箱格式不正确");
        return;
    }

    if (!vaildeParam(political)) {
        layer.msg("请选择政治面貌");
        return;
    }

    if (!vaildeParam(hometown)) {
        layer.msg("籍贯不能为空");
        return;
    }

    if (!vaildeParam(education)) {
        layer.msg("请选择文化程度");
        return;
    }

    if (!vaildeParam(school)) {
        layer.msg("毕业学校不能为空");
        return;
    }

    if (!vaildeParam(professional)) {
        layer.msg("专业不能为空");
        return;
    }

    if (!vaildeParam(date)) {
        layer.msg("毕业时间不能为空");
        return;
    }

    if (!vaildeParam(phone)) {
        layer.msg("电话不能为空");
        return;
    }

    if (!vaildeMobile(phone)) {
        layer.msg("电话格式不正确");
        return;
    }

    if (!vaildeParam(resume)) {
        layer.msg("请上传您的简历");
        return;
    }

    var api = '/api/apply/add';
    var data = {
        "id": id,
        "name": name,
        "sex": sex,
        "marriage": marriage,
        "mail": mail,
        "political": political,
        "hometown": hometown,
        "education": education,
        "school": school,
        "professional": professional,
        "date": date,
        "language": language,
        "phone": phone,
        "resume": resume,
        "recruitmentName": recruitmentName,
    }

    postJSON(api, data, function (res) {
        layer.msg('提交中...');
        setTimeout(function () {
            if ('success' === res.type) {
                layer.open({
                    type: 1,
                    content: '申请成功，符合要求会第一时间打电话或者发邮件给您',
                    btn: ['确定'],
                    closeBtn: 0,
                    yes: function (index, layero) {
                        layer.close(index);
                        if (type === "校招") {
                            location.href = '/campusRecruiting';
                        } else {
                            location.href = '/social';
                        }
                    }
                })


            } else if ('error' === res.type) {
                layer.msg(res.content);
            } else {
                layer.msg(res.content);
            }
        }, 1000)
    })


});